package cz.kfad.ofrss.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import com.actionbarsherlock.app.SherlockListActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import cz.kfad.ofrss.R;
import cz.kfad.ofrss.backend.BackendService;
import cz.kfad.ofrss.backend.IBackend;
import cz.kfad.ofrss.common.ApplicationContext;
import cz.kfad.ofrss.common.DoneCallback;
import cz.kfad.ofrss.common.ListAdapter;
import cz.kfad.ofrss.model.Category;
import cz.kfad.ofrss.util.LabelUtil;

import java.util.List;

public class CategoriesActivity extends SherlockListActivity {

    private IBackend backend = BackendService.createBackend(this);
    private DoneCallback done = new DoneCallback() {

        @Override
        public void done() {
            reload();
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.simple_list);
        setTitle(getString(R.string.categoryActivityTitle));

        reload();
        registerForContextMenu(getListView());
    }

    @Override
    protected void onResume() {
        super.onResume();
        reload();
        ApplicationContext appState = ((ApplicationContext) getApplicationContext());
        appState.setContextToAsynTasks(this, done);
    }

    public void reload() {
        final List<Category> allCategories = backend.findAllCategories();

        setListAdapter(new ListAdapter<Category>(this, allCategories) {
            @Override
            public long getItemId(int position) {
                return getItem(position).getId();
            }

            @Override
            public CharSequence getLabel(Category item) {
                return LabelUtil.toText(item);
            }

        });
    }


    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        final Intent intent = new Intent(this, CategoryActivity.class);
        intent.putExtra(CategoryActivity.CATEGORY_ID_KEY, (int) id);
        startActivity(intent);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(R.string.categories_menu_add).setIcon(android.R.drawable.ic_menu_add).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
        menu.add(R.string.feed_list_menu_refresh).setIcon(android.R.drawable.ic_menu_rotate).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) {
        final CharSequence itemTitle = item.getTitle();

        if (getResources().getString(R.string.feed_list_menu_refresh).equals(itemTitle)) {
            ApplicationContext appState = ((ApplicationContext) getApplicationContext());
            appState.executeAsynRefreshTask(this, done);
        } else if (getResources().getString(R.string.categories_menu_add).equals(itemTitle)) {
            startActivity(new Intent(this, AddCategoryActivity.class));
        } else {
            return false;
        }
        return true;
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        menu.add(R.string.category_contextMenu_edit);
        menu.add(R.string.category_contextMenu_delete);
    }

    @Override
    public boolean onContextItemSelected(android.view.MenuItem item) {
        final CharSequence itemTitle = item.getTitle();
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        int itemId = (int) getListAdapter().getItemId(info.position);

        if (getResources().getString(R.string.category_contextMenu_edit).equals(itemTitle)) {
            Intent intent = new Intent(this, AddCategoryActivity.class);
            intent.putExtra(AddCategoryActivity.CATEGORY_ID_KEY, itemId);
            startActivity(intent);

        } else if (getResources().getString(R.string.category_contextMenu_delete).equals(itemTitle)) {
            backend.deleteCategory(itemId);
            Toast.makeText(CategoriesActivity.this, getString(R.string.categoryDeleted), Toast.LENGTH_LONG).show();
            reload();
        } else {
            return false;
        }

        return true;
    }
}
